package com.example.demo.controller;


import com.example.comon.domain.R;
import com.example.comon.service.RedisCache;
import com.example.demo.domain.Bo.AddUserBo;
import com.example.demo.domain.Bo.UserListBo;
import com.example.demo.domain.User;
import com.example.demo.domain.Vo.StudentInforVo;
import com.example.demo.domain.Vo.UserListVo;
import com.example.demo.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/test")
@Api(value = "test",tags = "测试")
public class TestController {

    //private List<User> list = new ArrayList<>();//自定义一个存储容器来保存 。保存到内存，未持久保存（也就是磁盘里面）。


    @Autowired
    private IUserService userService;

    @Autowired
    private RedisCache redisCache;


    @GetMapping("/getUserList")
    @ApiOperation("获取用户列表")
    public R<List<UserListVo>> getUserList(UserListBo bo){
        String key ="userList";
        User user = new User();
        user.setAccount(bo.getAccount());
        List<UserListVo> list = redisCache.getCacheList(key);
        if(CollectionUtils.isEmpty(list)){
            list = userService.selectUser(user);
            redisCache.setCacheList(key, list);
        }
        return R.success(list);
    }



    @PostMapping("/addUser")
    @ApiOperation("增加用户")
    public R<User> addUser(@RequestBody  AddUserBo bo){
        User user = new User();
        user.setAccount(bo.getAccount());
        user.setPassword(bo.getPassword());
        userService.insertUser(user);
        return R.success(user);
    }

    @PostMapping("/updateUserByAccount")
    @ApiOperation("修改用户")
    public R updateUserByAccount(@RequestBody User user){
        userService.updateUserByAccount(user);
        return R.success();
    }

    @DeleteMapping("/delUserByAccount")
    @ApiOperation("删除用户")
    public R delUserByAccount(String account){
        userService.delUserByAccount(account);
        return R.success();
    }

    @GetMapping("/selectStudentInfor")
    @ApiOperation("获取学生列表")
    public R<List<StudentInforVo>> selectStudentInfor(){
        List<StudentInforVo> list =  userService.selectStudentInfor();
        return R.success(list);
    }

}
